package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    //处理新增请求
    @RequiresPermissions("department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
   // @RequirePermission(name="部门新增/编辑",expression = "department:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        try {
            if (department.getId() != null) {//修改
                departmentService.update(department);
            } else { //新增
                departmentService.save(department);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }

    }

    //用模态框,部门中的input不需要了
    //处理去新增请求
   /* @RequestMapping("/input")
    @RequirePermission(name="部门新增",expression = "department:input")
    public String input(Long id, Model model) {
        //若传 id 值,表示是去修改,那就要去查询被修改的部门对象,存在模型中
        if (id != null) { // 表示去修改
            Department department = departmentService.get(id);
            model.addAttribute("department", department);
        }
        // /WEB-INF/views/department/input.jsp
        return "department/input";
    }*/

    //处理删除部门请求
    @RequiresPermissions(value = {"department:delete","部门删除"},logical=Logical.OR)
    @RequestMapping("/delete")
    @ResponseBody
    //@RequirePermission(name="部门删除",expression = "department:delete")
    public JsonResult delete(Long id) {
        //传了id值就删除对应的部门数据

            if (id != null) {
                departmentService.delete(id);
            }

            return new JsonResult();
        }

    // 处理查询所有部门请求
    //允许配置多个权限表达式,默认的判断逻辑就是与(两个都必须要有)
    //为了方便获取权限的名称,可以把逻辑改为或or(其中一个有就可能访问)
    @RequiresPermissions(value = {"department:list","部门页面"},logical=Logical.OR)
    @RequestMapping("/list")
    //@RequirePermission(name="部门页面",expression = "department:list")
    public String list(Model model,QueryObject qo) {
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        //需要判断角色或者权限的时候,shiro会自动调用realm的doGetAuthorizationInfo方法,获取用户数据,进行对比
        System.out.println("admin:" + subject.hasRole("admin"));
        System.out.println("employee:delete:" + subject.isPermitted("employee:delete"));

        model.addAttribute("pageInfo", departmentService.query(qo));
        return "department/list"; // 找视图 /WEB-INF/views/department/list.jsp
    }
}